<template>
  <div class="footer">

    <!-- 底部播放器图片 -->
    <div class="m-img">
      <img v-lazy="this.$store.state.music.picUrl">
    </div>

    <!-- 底部播放器前进后退播放按钮 -->
    <div class="m-player">
      <div class="m-player-prev el-icon-caret-left"></div>
      <div class="m-player-play" :class="{'el-icon-video-play':$store.state.player,'el-icon-video-pause':!$store.state.player}" @click="selectPlayer"></div>
      <div class="m-player-next el-icon-caret-right"></div>
    </div>

    <!-- 底部播放器进度条 -->
    <div class="m-bar">
      <div class="m-bar-time">
        <div class="m-bar-time-left" v-show="this.$store.state.music.name!=''">
          <span>{{this.$store.state.music.name}}</span>
          <span>&nbsp;&nbsp;-&nbsp;&nbsp;</span>
          <span>{{this.$store.state.music.singer}}</span>
        </div>
        <div class="m-bar-time-right">
          <span>{{time*1000 | filterTime}}</span>
          <span>&nbsp;&nbsp;/&nbsp;&nbsp;</span>
          <span>{{this.$store.state.music.duration | filterTime}}</span>
        </div>
      </div>
      <div class="m-bar-slider">
        <el-slider v-model="time" :show-tooltip="false" :max="Math.floor(this.$store.state.music.duration)/1000" @change="selectTime"></el-slider>
      </div>
    </div>

    <!-- 底部播放器其他功能按钮 -->
    <div class="m-other">
      <!--收藏当前音乐-->
      <div class="el-icon-star-off" :class="{'m-other-like1':!selectlike,'m-other-like2':selectlike}" @click="selectLike"></div>
      <!--播放方式-->
      <div class="m-other-mode el-icon-refresh"></div>
      <!--音量-->
      <div class="m-other-sound el-icon-microphone" @click="selectvolume=!selectvolume"></div>
      <!--播放列表（历史记录）-->
      <div class="m-other-playlist el-icon-finished" @click="selectplaylist=!selectplaylist">
        <el-badge class="mark" :value="0" />
      </div>
    </div>

    <!-- 音量按钮的弹框 -->
    <div class="m-volume" :class="{'m-volume-opacity':selectvolume}">
      <!--这两个是实现弹出框的小三角形-->
      <span class="m-volume-sj1"></span>
      <span class="m-volume-sj2"></span>
      <!--图标-->
      <div class="m-volume-ico el-icon-microphone"></div>
      <!--滑块-->
      <div class="m-volume-slider">
        <el-slider v-model="volume" :show-tooltip="false" :max="100" @change="selectVolume" class="volume-slider"></el-slider>
      </div>
    </div>

    <!-- 播放列表弹框 -->
    <div class="m-playlistBox" v-show="selectplaylist">
      <div class="m-playlistBox-header">
        <div class="m-playlistBox-header-char"></div>
        <div class="m-playlistBox-header-text">播放列表</div>
        <div class="m-playlistBox-header-all"><i class="el-icon-folder-add"></i>收藏全部 </div>
        <div class="m-playlistBox-header-dele"><i class="el-icon-delete"></i>清空</div>
        <div class="m-playlistBox-header-close" @click="selectplaylist=!selectplaylist"><i class="el-icon-close"></i></div>
      </div>
      <div class="m-playlistBox-main">
        <el-scrollbar>
          <table>
            <tbody>
              <tr v-for="(item,index) in this.$store.state.playlist" :key="index" @click="selectcolumn=index" :class="{'m-playlistBox-clickrow':selectcolumn===index,'m-playlistBox-playrow':index===1}">
                <td class="m-playlistBox-column1"><i class="el-icon-caret-right" v-show="index===1"></i></td>
                <td class="m-playlistBox-column2 ellipsis"><p class="ellipsis">{{item.song}}</p></td>
                <td class="m-playlistBox-column3 ellipsis"><p class="ellipsis">{{item.singer}}</p></td>
                <td class="m-playlistBox-column4 ellipsis"><p class="ellipsis">{{item.time}}</p></td>
                <td class="m-playlistBox-column5"><i class="el-icon-circle-close" v-show="selectcolumn===index"></i></td>
              </tr>
            </tbody>
          </table>
        </el-scrollbar>
      </div>
    </div>

    <!-- audio -->
    <audio :src="this.$store.state.music.url" autoplay="autoplay" ref="myaudio"></audio>

  </div>
</template>
<script>
  export default {
    data() {
      return {
        time: 0, //播放动态时间
        timer: 0, //定义一个定时器
        selectlike: false, //false不收藏  true收藏
        selectvolume: false, //false 不弹出音量键  true弹出音量键
        selectplaylist: false, //false 不弹出播放列表  true反之
        volume: 100, //音量，默认是100
        selectcolumn: 0, //播放列表，点击行，为其添加类
      }
    },
    methods: {
      // 定时器函数，用来控制歌曲播放进度
      myfun() {
        this.timer = setInterval(() => {
          if (this.time === Math.floor(this.$refs.myaudio.duration)) {
            this.$store.state.player = true;
            this.$refs.myaudio.pause();
            this.time = 0;
            clearInterval(this.timer);
          } else {
            this.time = Math.floor(this.$refs.myaudio.currentTime);
            // console.log(this.time);
          }

        }, 1000);
      },
      // 控制播放暂停按钮
      selectPlayer(){
        this.$store.state.player = !this.$store.state.player;
      },
      selectPlay() {
        if (this.$store.state.player == true) {
          this.$refs.myaudio.pause();
          clearInterval(this.timer); //当歌曲暂停，清除定时器
        } else {
          this.$refs.myaudio.play();
          this.myfun(); //当歌曲播放，调用定时器函数
        }
      },

      // 拖动进度条，选择播放位置
      selectTime() {
        // this.selectPlay();
        this.$refs.myaudio.currentTime = this.time;
      },

      //点击收藏
      selectLike() {
        if (this.selectlike === false) {
          this.selectlike = true;
          this.$message({
            message: "已添加到我喜欢的音乐",
            type: 'success',
            duration: 1200,
            offset: 400,
            center: true
          });
        } else {
          this.selectlike = false;
          this.$message({
            message: "已取消喜欢",
            type: 'error',
            duration: 1200,
            offset: 400,
            center: true
          });
        }

      },

      // 音量
      selectVolume() {
        this.$refs.myaudio.volume = this.volume / 100;
      },


    },
    mounted() {
      // 初始化音量，默认为100
      this.selectVolume();
    },

    computed:{
      // 监听播放
      isPlay(){
        return this.$store.state.player;
      }
    },

    watch:{
      isPlay(newval,oldval) {
          this.selectPlay();
          // console.log(newval);
      }
    }
  }
</script>
<style scoped="scoped">
  .footer {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
  }

  /* 图片 */
  .m-img {
    width: 60px;
    height: 60px;
    border: 1px solid #f2f2f2;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .m-img img {
    display: block;
    width: 100%;
  }

  /* 播放按钮 */
  .m-player {
    width: 188px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0px 30px;
    color: #FFFFFF;
  }

  .m-player>div {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
  }

  .m-player .m-player-prev {
    width: 28px;
    height: 28px;
    background-color: #DF3B3B;
    border-radius: 50%;
  }

  .m-player .m-player-play {
    width: 32px;
    height: 32px;
    background-color: #DF3B3B;
    border-radius: 50%;
    font-size: 43px;
  }

  .m-player .m-player-next {
    width: 28px;
    height: 28px;
    background-color: #DF3B3B;
    border-radius: 50%;
  }

  /* 进度条 */
  .m-bar {
    flex: 1;
    height: 100%;
    padding-right: 20px;
  }

  .m-bar-time {
    height: 50%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
  }

  .m-bar-time>div {
    flex: 1;
    height: 100%;
    display: flex;
    align-items: center;
  }

  .m-bar-time>.m-bar-time-left {
    justify-content: flex-start;
    color: #999;
  }

  .m-bar-time>.m-bar-time-right {
    justify-content: flex-end;
    color: #999;
  }

  .m-bar-time-left>span:nth-of-type(1),
  .m-bar-time-right>span:nth-of-type(1) {
    color: #666;
  }

  .m-bar-time-left>span:nth-of-type(3),
  .m-bar-time-right>span:nth-of-type(3) {
    color: #999;
  }

  .m-bar-slider {
    height: 50%;
    width: 100%;
    padding-left: 8px;
  }

  >>>.el-slider__runway {
    height: 2px;
    background-color: #e5e5e5;
    margin: 8px 0px;
  }

  >>>.el-slider__bar {
    height: 2px;
    background-color: #D13C3A;
    margin: 0px;
  }

  >>>.el-slider__button-wrapper {
    width: auto;
    height: auto;
    top: -8px;
  }

  >>>.el-slider__button {
    width: 16px;
    height: 16px;
    border: 1px solid #e1e1e1;
    position: relative;
  }

  >>>.el-slider__button::before {
    content: "";
    position: absolute;
    margin: auto;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #D13C3A;
    width: 4px;
    height: 4px;
    border-radius: 50%;
  }

  /* 播放其他功能按钮 */
  .m-other {
    width: 262px;
    height: 100%;
    display: flex;
    align-items: center;
    padding-right: 30px;
  }

  .m-other>div {
    flex: 1;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: #999;
    /* border-left: 1px solid #F3F3F5; */
  }

  >>>.el-badge {
    display: flex;
    align-items: center;
    padding: 0px 3px;
  }

  >>>.el-badge__content {
    height: 14px;
    line-height: 14px;
    background-color: #BBBCC1;
    border-radius: 10px;
    padding: 1px 2px;
  }

  .m-other .m-other-like1 {
    color: #999999;
  }

  .m-other .m-other-like2 {
    color: #DF3B3B;
  }

  /* 音量 */
  .m-volume {
    width: 295px;
    height: 50px;
    border: 1px solid #CECED4;
    position: fixed;
    right: 10px;
    bottom: 60px;
    background-color: #FFFFFF;
    opacity: 0;
    z-index: 100;
    transition: all .2s;
    display: flex;
    align-items: center;
  }

  .m-volume .m-volume-sj1 {
    position: absolute;
    width: 0;
    height: 0;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 10px solid #CECED4;
    left: 177px;
    top: 48px;
  }

  .m-volume .m-volume-sj2 {
    position: absolute;
    width: 0;
    height: 0;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 10px solid #FFFFFF;
    left: 177px;
    top: 46px;
  }

  .m-volume-opacity {
    opacity: 1;
    bottom: 70px;
  }

  .m-volume .m-volume-ico {
    width: 36px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #999999;
    font-size: 18px;
  }

  .m-volume .m-volume-slider {
    flex: 1;
    height: 100%;
    display: flex;
    align-items: center;
    padding: 0px 20px 0px 10px;
  }

  .m-volume .m-volume-slider .volume-slider {
    width: 100%;
  }

  .m-playlistBox {
    width: 475px;
    height: 425px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    position: fixed;
    right: 5px;
    bottom: 65px;
    opacity: 1;
    transition: all .2s;
    z-index: 101;
    background-color: #FFFFFF;
    overflow: hidden;
  }

  .m-playlistBox .m-playlistBox-header {
    width: 100%;
    height: 45px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    color: #333;
  }

  .m-playlistBox-header .m-playlistBox-header-char {
    width: 30px;
    height: 100%;
    display: flex;
    align-items: center;
  }

  .m-playlistBox-header .m-playlistBox-header-text {
    flex: 1;
    height: 100%;
    display: flex;
    align-items: center;
  }

  .m-playlistBox-header .m-playlistBox-header-all {
    width: 88px;
    height: 100%;
    display: flex;
    align-items: center;
  }

  .m-playlistBox-header .m-playlistBox-header-dele {
    width: 70px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .m-playlistBox-header .m-playlistBox-header-close {
    width: 54px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .m-playlistBox-header div i {
    font-size: 16px;
    margin-right: 3px;
  }

  .m-playlistBox .m-playlistBox-main {
    width: 100%;
    position: absolute;
    top: 45px;
    bottom: 0;
    left: 0;
    right: 0;
  }
  .m-playlistBox-main table,
  .m-playlistBox-main table tbody,
  .m-playlistBox-main table tbody tr{
    width: 100%;
    border-spacing: 0;
  }
  .m-playlistBox-main table tr{
    display: flex;
    align-items: center;
  }
  .m-playlistBox-main table tr:hover{
    background-color: #f2f2f3;
  }
  .m-playlistBox-main table tr:nth-of-type(2n-1){
    background-color: #f9f9f9;
  }
  .m-playlistBox-main table tr:nth-of-type(2n-1):hover{
    background-color: #f2f2f3;
  }
  .m-playlistBox-main table tr td{
      height: 45px;
      padding: 0px;
  }
  .m-playlistBox-main .m-playlistBox-column1{
    width: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0;
    font-size: 18px;
  }
  .m-playlistBox-main .m-playlistBox-column2{
    max-width: 231px;
    flex: 1;
    display: flex;
    align-items: center;
    color: #333;
    padding-right: 20px;
    box-sizing: border-box;
    overflow: hidden;
  }
  .m-playlistBox-main .m-playlistBox-column3{
    width: 90px;
    display: flex;
    align-items: center;
    font-size: 12px;
  }
  .m-playlistBox-main .m-playlistBox-column4{
    width: 68px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
  }
  .m-playlistBox-main .m-playlistBox-column5{
    width: 54px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: #999;
  }

  /* 歌曲列表。点击某个歌曲行，为其添加类 */
  .m-playlistBox-main table .m-playlistBox-clickrow td{
    background-color: #e8e8ea;
  }
  .m-playlistBox-main table .m-playlistBox-clickrow:hover td{
    background-color: #dedee0;
  }

  /* 歌曲列表，如果播放的是当前某首音乐，则为其添加类 */
  .m-playlistBox-main table .m-playlistBox-playrow {
    color: #c62e2e;
    font-weight: bold;
  }
  .m-playlistBox-main table .m-playlistBox-playrow .m-playlistBox-column2{
    color: #c62e2e;
  }
</style>
